<?php

class CfComposerController extends AController {

    public function init() {
        parent::init();
        SpsState::setPrefix('DCfComposer');
    }

    /* ------------------------------------------------------------------------
      BASIC FUNCTIONS
    -------------------------------------------------------------------------*/

    public function actionIndex() {
        if (Yii::app()->request->isAjaxRequest) {
            SpsState::writeState();
        }

        $model = new DCfComposer('search');
        $model->unsetAttributes();

        if (isset($_GET['DCfComposer']))
            $model->attributes = $_GET['DCfComposer'];
        elseif (SpsState::hasState('search'))
            $model->attributes = SpsState::getState('search');

        $this->render('index', array(
            'model' => $model,
        ));
    }

    public function actionCreate() {
        $model = new DCfComposer;

        if (isset($_POST['DCfComposer'])) {
            $model->attributes = $_POST['DCfComposer'];

            if ( $model->save() ) {
                Yii::app()->user->setFlash('message', 'created');
                $this->redirect(array('index'));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        Yii::app()->user->setFlash('keepState', true);
        $model = $this->loadModel($id);

        if (isset($_POST['DCfComposer'])) {
            $model->attributes = $_POST['DCfComposer'];
            if ($model->save()) {
                Yii::app()->user->setFlash('message', 'updated');
                $this->redirect(array('index'));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isAjaxRequest) {
            $this->loadModel($id)->delete();
        } else {
            throw new CHttpException(400, Yii::t('DadminModule.message', 'invalid_request'));
        }
    }

    public function loadModel($id) {
        $model = DCfComposer::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
    /* ------------------------------------------------------------------------
      //BASIC FUNCTIONS
    -------------------------------------------------------------------------*/
}